<%@tag import="java.sql.Date"%>
<%@tag import="by.epam.communalservices.entity.JkhObject"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhObject"%>
<%@tag import="java.sql.Connection"%>
<%@tag import="org.apache.log4j.Logger"%>

<%@ attribute name="id" required="true"%>
<%@ attribute name="idOwner" required="true"%>
<%@ attribute name="address" required="true"%>
<%@ attribute name="name" required="true"%>
<%@ attribute name="surname" required="true"%>
<%@ attribute name="patronymic" required="true"%>
<%@ attribute name="date_last_repair" required="true"%>
<%@ attribute name="delete" required="true"%>
<%@ attribute name="submit" required="true"%>
<%
    
    Logger objectEditLogger = Logger.getLogger("objectEditLogger");
    request.setAttribute("message", "");

    Connection con = (Connection) request.getSession().getAttribute("currentConnection");
    JkhObject jkhObject = null;
    JdbcDaoJkhObject daoObject = new JdbcDaoJkhObject(con);

    try {
        if (getJspContext().getAttribute("id") != ""
                && Long.parseLong(getJspContext().getAttribute("id").toString()) > 0) {
            jkhObject = daoObject.read(Long.parseLong(getJspContext().getAttribute("id").toString()));
            if (getJspContext().getAttribute("submit") != "") {
                
                if (getJspContext().getAttribute("idOwner").toString() != "") {
                	jkhObject.setIdOwner(Long.parseLong(getJspContext().getAttribute("idOwner").toString()));
                }
                jkhObject.setAddress(getJspContext().getAttribute("address").toString());
                jkhObject.setName(getJspContext().getAttribute("name").toString());
                jkhObject.setSurname(getJspContext().getAttribute("surname").toString());
                jkhObject.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                try {
                    jkhObject.setDateLastRepair(Date.valueOf(getJspContext().getAttribute("date_last_repair")
                            .toString().replace(".", "-")));
                } catch (Exception e) {

                }
                daoObject.update(jkhObject.getId(), jkhObject);
                request.setAttribute("message", "Object updated");
            }
        } else if (getJspContext().getAttribute("delete") != ""
                && Long.parseLong(getJspContext().getAttribute("delete").toString()) > 0) {
            daoObject.delete(Long.parseLong(getJspContext().getAttribute("delete").toString()));
            request.setAttribute("message", "Object deleted");
        } else {
            jkhObject = new JkhObject();
            if (getJspContext().getAttribute("submit") != "") {
                jkhObject.setIdOwner(Long.parseLong(getJspContext().getAttribute("idOwner").toString()));
                jkhObject.setAddress(getJspContext().getAttribute("address").toString());
                jkhObject.setName(getJspContext().getAttribute("name").toString());
                jkhObject.setSurname(getJspContext().getAttribute("surname").toString());
                jkhObject.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                try {
                    jkhObject.setDateLastRepair(Date.valueOf(getJspContext().getAttribute("date_last_repair")
                            .toString().replace(".", "-")));
                } catch (Exception e) {
                    jkhObject.setDateLastRepair(new Date(System.currentTimeMillis()));
                    objectEditLogger.error("Failed to set date",e);
                }
                jkhObject.setId(daoObject.create(jkhObject));
                request.setAttribute("message", "Object Created");
            } else {
                jkhObject.setDateLastRepair(new Date(System.currentTimeMillis()));
            }
        }

    } catch (Exception ex) {
        objectEditLogger.error("Failed edit",ex);
        out.println(ex.getMessage() + "<br>");
        for (StackTraceElement el : ex.getStackTrace()) {
            out.println(el.toString() + "<br>");
        }
        request.setAttribute("message", "Error input parameter");
    }
    
    if (jkhObject == null) {
        request.setAttribute("form", false);
        if (getJspContext().getAttribute("delete") == "") {
            request.setAttribute("message", "Object Not Found");
        }
    } else {
        request.setAttribute("form", true);
        request.setAttribute("object", jkhObject);
    }
%>